import React from 'react';
import { Redirect } from 'react-router-dom';
import { Route } from 'react-router-dom';
import { hasToken } from 'utils/strorage';

export default class AuthRoute extends React.Component {
  render() {
    // 剩余属性：...restProps表示除了以结构的属性，剩下的属性存在restProps对象上
    // 1.使用剩余属性 ...restProps
    const { path, component: Component, ...restProps } = this.props;
    return (
      <Route
        // 2. 将剩余组成的对象，放在Route身上
        {...restProps}
        path={path}
        render={(_props) => {
          // console.log('_props  ----->  ', _props);
          if (hasToken()) {
            return <Component {..._props} />;
          }
          return <Redirect to="/login" />;
        }}
      ></Route>
    );
  }
}
